<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <title>短信验证码注册</title>
  <style>
    body {
      font-family: "Segoe UI", sans-serif;
      background-color: #f0f6ff;
      margin: 0;
      padding: 0;
    }

    .container {
      max-width: 500px;
      margin: 100px auto;
      background-color: #ffffff;
      padding: 40px 50px;
      border-radius: 16px;
      box-shadow: 0 12px 32px rgba(0, 0, 0, 0.1);
    }

    .logo {
      text-align: center;
      font-size: 40px;
      margin-bottom: 10px;
    }

    h2 {
      text-align: center;
      color: #1e2f45;
      margin-bottom: 30px;
      font-size: 24px;
    }

    .form-group {
      margin-bottom: 24px;
    }

    label {
      display: block;
      font-weight: 600;
      margin-bottom: 8px;
      color: #333;
    }

    input {
      width: 100%;
      padding: 12px;
      border: 1px solid #ccd6e0;
      border-radius: 6px;
      background-color: #eef4ff;
      font-size: 14px;
      transition: border-color 0.3s, box-shadow 0.3s;
    }

    input:focus {
      outline: none;
      border-color: #5b9dff;
      background-color: #eaf2ff;
      box-shadow: 0 0 4px rgba(91, 157, 255, 0.5);
    }

    .row-inline {
      display: flex;
      gap: 10px;
    }

    .row-inline input {
      flex: 1;
    }

    .btn {
      width: 100%;
      padding: 12px;
      background-color: #dbe7ff;
      border: none;
      border-radius: 6px;
      font-weight: bold;
      color: #2b3f5c;
      cursor: pointer;
      transition: background-color 0.2s;
    }

    .btn:hover {
      background-color: #c3d8ff;
    }

    .code-msg {
      margin-top: 8px;
      color: green;
      font-size: 14px;
      text-align: center;
    }

    .link {
      text-align: center;
      margin-top: 24px;
    }

    .link a {
      color: #337ab7;
      text-decoration: none;
    }

    .link a:hover {
      text-decoration: underline;
    }
  </style>
</head>
<body>

<div class="container">
  <div class="logo">🏦</div>
  <h2>短信验证码注册</h2>

      <form id="register-form" method="POST" action="{{ url_for('register_sms') }}">
      <div class="form-group">
        <label>姓名：</label>
        <input type="text" name="name" required>
      </div>

      <div class="form-group">
        <label>身份证号：</label>
        <input type="text" name="pid" required>
      </div>

      <div class="form-group">
        <label>手机号：</label>
        <input type="text" name="phone" required>
      </div>

      <div class="form-group">
        <label>地址（可选）：</label>
        <input type="text" name="address">
      </div>

      <div class="form-group">
      <label>验证码：</label>
      <input type="text" name="code" required placeholder="请输入验证码" style="width: 100%; margin-top: 6px;">
      <button class="btn" type="button" onclick="sendCode()" style="margin-top: 10px; width: 100%;">获取验证码</button>
      <div class="code-msg" id="debug-code"></div>
    </div>




      <button class="btn" type="button" onclick="registerSms()">注册</button>
    </form>


  <div class="link">
    <p><a href="{{ url_for('login_sms') }}">已有账号？去登录</a></p>
    <p><a href="{{ url_for('login') }}">返回普通登录</a></p>
  </div>
</div>
<script>
function sendCode() {
  const phone = document.querySelector('input[name="phone"]').value.trim();
  const pid = document.querySelector('input[name="pid"]').value.trim();
  if (!phone || !pid) {
    alert("请先填写手机号和身份证号！");
    return;
  }

  fetch(`/send_sms_code?phone=${phone}&pid=${pid}&mode=register`)
    .then(res => res.text())
    .then(text => {
      document.getElementById("debug-code").innerText = text;
    })
    .catch(err => {
      document.getElementById("debug-code").innerText = "发送失败：" + err;
    });
}


function registerSms() {
  const form = document.getElementById('register-form');
  const formData = new FormData(form);

  fetch('/register_sms', {
    method: 'POST',
    body: formData
  })
  .then(res => res.json())
  .then(data => {
    alert(data.message);
    if (data.status === 'success') {
      window.location.href = '/login_sms'; // 注册成功跳转到登录页面
    }
  })
  .catch(err => {
    alert('请求失败，请稍后重试');
    console.error(err);
  });
}
</script>

</body>
</html>